/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wdullaer.myapplication;

import at.markushi.ui.ActionView;
import at.markushi.ui.RevealColorView;
import at.markushi.ui.action.BackAction;
import at.markushi.ui.action.DrawerAction;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.utils.Point;

/**
 * 主页面
 */
public class MainAbility extends Ability implements Component.ClickedListener {
    private boolean isActionView = true;
    private RevealColorView revealColorView;
    private Component selectedView;
    private RgbColor backgroundColor;
    private int mDuration = 300;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        revealColorView = (RevealColorView) findComponentById(ResourceTable.Id_reveal);
        backgroundColor = new RgbColor(0, 0, 0);

        findComponentById(ResourceTable.Id_btn_1).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_2).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_3).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_4).setClickedListener(this);

        ActionView actionView = (ActionView) findComponentById(ResourceTable.Id_action);
        actionView.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (isActionView) {
                    actionView.setAction(new BackAction(), ActionView.ROTATE_COUNTER_CLOCKWISE);
                } else {
                    actionView.setAction(new DrawerAction(), ActionView.ROTATE_COUNTER_CLOCKWISE);
                }
                isActionView = !isActionView;
            }
        });
    }

    @Override
    public void onClick(Component component) {
        RgbColor color = new RgbColor(255, 255, 255);
        switch (component.getId()) {
            case ResourceTable.Id_btn_1:
                color = new RgbColor(255, 0, 255);
                break;
            case ResourceTable.Id_btn_2:
                color = new RgbColor(0, 255, 255);
                break;
            case ResourceTable.Id_btn_3:
                color = new RgbColor(255, 0, 0);
                break;
            case ResourceTable.Id_btn_4:
                color = new RgbColor(0, 0, 255);
                break;
        }
        final Point point = getLocationInView(revealColorView, component);
        if (selectedView == component) {
            revealColorView.hide((int) point.getPointX(), (int) point.getPointY(), backgroundColor, 0, mDuration, null);
            selectedView = null;
        } else {
            revealColorView.reveal((int) point.getPointX(), (int) point.getPointY(), color, component.getHeight() / 2, mDuration, null);
            selectedView = component;
        }
    }

    private Point getLocationInView(Component src, Component target) {
        final int[] l0 = src.getLocationOnScreen();
        final int[] l1 = target.getLocationOnScreen();

        l1[0] = l1[0] - l0[0] + target.getWidth() / 2;
        l1[1] = l1[1] - l0[1] + target.getHeight() / 2;

        return new Point(l1[0], l1[1]);
    }
}





